package com.huluobo.jetpackdemo.flow

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.huluobo.jetpackdemo.R
import com.huluobo.jetpackdemo.hilt.UserBean
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch

class FlowActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_flow)
        CoroutineScope(Job()).launch {
            //接受数据
            loadData2().collect {
                Log.i("TagA", "int:$it")
            }
        }
    }

    private fun loadData() = flow {
        Log.i("TagA", "进入loadData方法")
        for (i in 1..10) {
            kotlinx.coroutines.delay(1000)
            emit(i)//发送数据
        }
    }

    private fun loadData2() = flow {
        for (i in 1..20) {
            emit(i)
            kotlinx.coroutines.delay(1000)
        }
    }.filter {
        //过滤,可以把数据进行判断,找出符合判断的值进行输出
        it % 2 != 0
    }.map {
        //转化,映射
        UserBean("LC", "123", it)
    }
}